#include "RDATest.h"


int main(int argc, char* argv[])
{
    GLEngine* engine = new GLEngine("RDATest",
                                    100, 100, 32, false, 0, 0);
    engine->setMaxFPS(100);
    engine->createClient<RDATest>("rdatest");
    engine->runClient("rdatest");
    delete engine;
}


void CPlayer::onUpdate(float dt)
{
    bool b = updateAnim(dt, cur_anim == 0);
    if ((b || cur_anim == 0) && next_anim != cur_anim)
    {
        setAnimation(&anims[next_anim]);
        cur_anim = next_anim;
    }
    if (b && cur_anim != 0)
    {
        //end animation
        vector2f np = v2projectXY(getCurrentFrame().rot[RP_Root]);
        float a = np.angle() + mDegToRad(rotation.z);
        float d = np.length();
        setPosition(position.x + cos(a)*d, position.y + sin(a)*d, position.z);
        setAnimation(&anims[0]);
        cur_anim = next_anim = 0;
    }
}

void CPlayer::initAnims(RagdollAnimation *panims)
{
    anims = panims;
    setAnimation(&anims[0]);
    cur_anim = next_anim = 0;
}

void CPlayer::setAction(int act)
{
    next_anim = act;
}

RDATest::RDATest(GLEngine* engine) :
    GLClient(engine)
{
}

RDATest::~RDATest()
{
}

void RDATest::onEnter()
{
    //scene = new SceneGraph(getEngine());
    scene = new GLScene();

    camera.zoom = -5;
    camera.rotation.set(-60, 0, -70);
    camera.position.set(0, 0, 1);

    anims[0].loadAnimation("anims/sword_stand.anim");
    anims[1].loadAnimation("anims/sword_chop.anim", 4);
    anims[2].loadAnimation("anims/sword_swing_left.anim", 4);
    anims[3].loadAnimation("anims/sword_swing_right.anim", 4);
    anims[4].loadAnimation("anims/sword_trust.anim", 4);

    player = scene->createObject<CPlayer>();
    player->initAnims(anims);
    player->getBodyPart(RP_RightHand)
        ->createObject()
        ->setModel(getModel("gfx/sword.obj"));
    player->setRotation(0, 0, 45);
    player->createBehavior<Oreol>();

    /*for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            scene->createObject()
                ->setColor(Color::Red)
                ->importXml("body.xml")
                ->setPosition(i, j, 0);
        }
    }*/

    /*scene->createObject()
        ->setPosition(1, 1, 0)
        ->setBoxSize(vector3f(0.5, 0.5, 1))
    ->createObject()
        ->setPosition(0, 0, 0.5)
        ->setRotation(90, 0, 0)
        ->setBoxSize(vector3f(0.25, 0.25, 3));
    scene->createObject()
        ->setPosition(1, 2.5, 0)
        ->setBoxSize(vector3f(0.5, 0.5, 1));*/
}

void RDATest::onLeave()
{
}

void RDATest::onEvent(GLEvent e)
{
    switch (e.type)
    {
    case EVT_KeyPress:
        if (e.key.code == KEY_ESC)
        {
            getEngine()->quit(0);
        }
        else if (e.key.code == KEY_W)
        {
            player->setAction(1);
        }
        else if (e.key.code == KEY_A)
        {
            player->setAction(2);
        }
        else if (e.key.code == KEY_D)
        {
            player->setAction(3);
        }
        else if (e.key.code == KEY_S)
        {
            player->setAction(4);
        }
        break;
    case EVT_MouseMove:
        {
            int dx = e.mouse.x - mouse.x;
            int dy = e.mouse.y - mouse.y;
            if (mouse.button[MOUSE_RIGHT])
            {
                camera.rotation.z += dx;
                camera.rotation.x += dy;
            }
            if (mouse.button[MOUSE_LEFT])
            {
                camera.translate2D(-dx*0.1, dy*0.1);
            }
        }
        break;
    case EVT_MouseWheel:
        {
            camera.zoom -= e.mouse.wheel;
        }
        break;
    default:
        break;
    }
}

void RDATest::onUpdate(float dt)
{
    scene->update(dt);
}

void RDATest::onDraw()
{
    Color cm[4] = { 0xFF000000, 0xFF000000, 0xFF000088, 0xFF000088 };
    rectf r(0, 0, 800, 600);

    glBeginSceneGUI(800, 600);
    glFillRectCM(r, cm);
    glEndSceneGUI();

    scene->renderScene(camera, 0);
}





